package com.example.tao.smartcar.view.indicator;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;

import com.example.tao.smartcar.R;

/**
 * Created by Tao on 2016/7/31.
 */
public class IndicatorView extends View {

    private int mValue = 0, mMax = 40, mMin = -40;

    private Paint panelPaint, indicatorPaint;
    private RectF panelRectF, indicatorRectF;
    private PointF centerPointF;
    private int viewHeight, viewWidth, oldViewHeight, oldViewWidth, panelRadius;

    private float scale = 1.0f;
    private int indicatorDegree = 0;

    public IndicatorView(Context context) {
        this(context, null);
    }

    public IndicatorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        panelPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        indicatorPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        centerPointF = new PointF();

        panelPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        panelPaint.setColor(Color.WHITE);
        indicatorPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        indicatorPaint.setColor(Color.WHITE);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewHeight = h;
        viewWidth = w;
        centerPointF.set(viewHeight/2, viewWidth/2);

        invalidateView();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackground(canvas);
        drawPanel(canvas);
    }

    private void drawBackground(Canvas canvas) {
        canvas.drawColor(getResources().getColor(R.color.white_00_alpha));
    }

    private void drawPanel(Canvas canvas) {
        Drawable drawable = getResources().getDrawable(R.mipmap.speed_panel);
        BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
        Bitmap bitmap = bitmapDrawable.getBitmap();

        if (((float)viewWidth)/((float)bitmap.getWidth()) > ((float)viewHeight)/((float)bitmap.getHeight())) {
            scale = ((float)viewWidth)/((float)bitmap.getWidth());
        } else {
            scale = ((float)viewHeight)/((float)bitmap.getHeight());
        }
        panelRectF = new RectF(0, 0, viewWidth, viewHeight);

        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

        canvas.drawBitmap(scaledBitmap, 0, 0, panelPaint);

        drawIndicator(canvas);
    }

    private void drawIndicator(Canvas canvas) {
        Drawable drawable = getResources().getDrawable(R.mipmap.speed_pointer);
        BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
        Bitmap bitmap = bitmapDrawable.getBitmap();

        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        //matrix.postRotate(-30, centerPointF.x, centerPointF.x);
        Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        canvas.save();
        canvas.rotate(indicatorDegree, centerPointF.x, centerPointF.y);
        canvas.drawBitmap(scaledBitmap, 0, 0, panelPaint);
        canvas.restore();
    }

    public void setValue(int value) {
        this.mValue = value;
        updateIndicator();
    }

    public void setMax(int value) {
        this.mMax = value;
        updateIndicator();
    }

    public void setMin(int value) {
        this.mMin = value;
        updateIndicator();
    }

    private void updateIndicator() {
        indicatorDegree = -134 + (int)(2*134*(((float)(mValue - mMin))/((float)(mMax - mMin))));
        invalidateView();
    }

    private void invalidateView() {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            invalidate();
        } else {
            postInvalidate();
        }
    }
}
